package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/path-sum/description/
 * 通过
 *
 * @author yulongTian
 * @create 2024-06-24 15:56
 */
public class Test48_1 {
    public static void main(String[] args) {

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    boolean ans = false;

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        preTravel(root, 0, targetSum);
        return ans;
    }

    private void preTravel(TreeNode root, int tempSum, int targetSum) {
        if (root.left == null && root.right == null) {
            if (tempSum + root.val == targetSum) {
                ans = true;
            }
            return;
        }
        //根
        tempSum += root.val;
        //左
        if (root.left != null) {
            preTravel(root.left, tempSum, targetSum);
        }
        //右
        if (root.right != null) {
            preTravel(root.right, tempSum, targetSum);
        }
    }

}
